﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


public class UIHelper
{
    public static TextBlock GetTextBlockByName(FrameworkElement element, string name, ref TextBlock tb)
    {
        int count = VisualTreeHelper.GetChildrenCount(element);
        FrameworkElement child;
        for (int i = 0; i < count; i++)
        {
            child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

            if (child != null)
            {
                if (child.GetType().Name == "TextBlock" & child.Name == name)
                {
                    tb = (TextBlock)child;
                }
                GetTextBlockByName(child, name, ref tb);
            }
        }
        return tb;
    }

    public static void GetChildByName<T>(FrameworkElement parent, string name, ref T control) where T : FrameworkElement
    {
        int count = VisualTreeHelper.GetChildrenCount(parent);
        FrameworkElement child;
        for (int i = 0; i < count; i++)
        {
            child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;

            if (child != null)
            {
                if (child.Name == name)
                {
                    control = child as T;
                    return;

                    //return control;
                }
                GetChildByName<T>(child, name, ref control);
            }
        }
        //return control;
    }

    //public static T GetChildByName<T>(DependencyObject parent, string name)
    //     where T : DependencyObject
    //{
    //    T obj = RecGetChildByName<T>(parent, name) as T;
    //    if (obj == null) throw new Exception("could find control "
    //                            + "of name as child");
    //    return obj;
    //}

    //private static DependencyObject RecGetChildByName<T>(DependencyObject parent,
    //             string name)
    //{
    //    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    //    {
    //        var child = VisualTreeHelper.GetChild(parent, i);

    //        Control childControl = child as Control;
    //        if (childControl != null)
    //        {
    //            if (childControl.Name == name) return child;
    //        }

    //        if (VisualTreeHelper.GetChildrenCount(child) > 0)
    //            return RecGetChildByName<T>(child, name);
    //    }

    //    return null;
    //}

    /// <summary>
    /// Finds a parent of a given item on the visual tree.
    /// </summary>
    /// <typeparam name="T">The type of the queried item.</typeparam>
    /// <param name="child">A direct or indirect child of the queried item.</param>
    /// <returns>The first parent item that matches the submitted type parameter. 
    /// If not matching item can be found, a null reference is being returned.</returns>
    public static T FindVisualParent<T>(DependencyObject child)
      where T : DependencyObject
    {
        // get parent item
        DependencyObject parentObject = VisualTreeHelper.GetParent(child);

        // we’ve reached the end of the tree
        if (parentObject == null) return null;

        // check if the parent matches the type we’re looking for
        T parent = parentObject as T;
        if (parent != null)
        {
            return parent;
        }
        else
        {
            // use recursion to proceed with next level
            return FindVisualParent<T>(parentObject);
        }
    }

    // Helper to search up the VisualTree
    public static T FindAnchestor<T>(DependencyObject current)
        where T : DependencyObject
    {
        do
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        }
        while (current != null);
        return null;
    }
}
